﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace FDEE.Model
{
    class CPTE3 : CPTMBase
    {
        Dictionary<double, List<int>> dic = new Dictionary<double, List<int>>();
        Dictionary<double, List<int>> addDic = new Dictionary<double, List<int>>();
        bool isAdd = false;
        internal CPTE3(string typeName, string pierNumber, eColumnType ct, eSubType st,
            int ww, int ll, int hh, int rr, double up, double low,
            eDB mainDB, string listCString = "", string listAddCString = "")
            : base(typeName, pierNumber, ct, st, ww, ll, hh, rr, up, low, mainDB)
        {
            if (st == eSubType.BM)
            {
                //listCString = "{(1,5)|248}";             
                if (!string.IsNullOrWhiteSpace(listCString))
                {
                    List<string> listC = listCString.Split(';').ToList();
                    List<string> listAddC = listAddCString.Split(';').ToList();
                    Dictionary<double, List<int>> dicV = new Dictionary<double, List<int>>();

                    foreach (var item in listC)
                    {
                        string[] values = item.Replace("{", "").Replace("}", "").Replace("(","").Replace(")", "").Replace("（", "").Replace("）", "").Split('|');
                        double vk = double.Parse(values[1]);
                        string[] vs = values[0].Split(',');
                        List<int> v = new List<int>();
                        foreach (var vl in vs)
                        {
                            v.Add(int.Parse(vl));
                        }
                        if (!dicV.Keys.Contains(vk))
                            dicV.Add(vk, v);
                        //dic = dicV.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                        dic = dicV.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                    }
                    if (!string.IsNullOrWhiteSpace(listAddCString))
                    {
                        Dictionary<double, List<int>> adddicV = new Dictionary<double, List<int>>();
                        foreach (var item in listAddC)
                        {
                            string[] values = item.Replace("{", "").Replace("}", "").Replace("(","").Replace(")", "").Replace("（", "").Replace("）", "").Split('|');
                            double vk = double.Parse(values[1]);
                            string[] vs = values[0].Split(',');
                            List<int> v = new List<int>();
                            foreach (var vl in vs)
                            {
                                v.Add(int.Parse(vl));
                            }
                            if (!adddicV.Keys.Contains(vk))
                                adddicV.Add(vk, v);
                            //addDic = adddicV.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                            addDic = adddicV.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                        }
                    }
                }
            }
            
            else
            {
                if (!string.IsNullOrWhiteSpace(listCString))
                {
                    List<string> listC = listCString.Split(';').ToList();
                    List<string> listAddC = listAddCString.Split(';').ToList();
                    Dictionary<double, List<int>> dicV = new Dictionary<double, List<int>>();

                    foreach (var item in listC)
                    {
                        string[] values = item.Replace("{", "").Replace("}", "").Replace("(","").Replace(")", "").Replace("（", "").Replace("）", "").Split('|');
                        double vk = double.Parse(values[1]);
                        string[] vs = values[0].Split(',');
                        List<int> v = new List<int>();
                        foreach (var vl in vs)
                        {
                            v.Add(int.Parse(vl));
                        }
                        if (!dicV.Keys.Contains(vk))
                            dicV.Add(vk, v);
                        dic = dicV.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                    }
                    Dictionary<double, List<int>> adddicV = new Dictionary<double, List<int>>();
                    foreach (var item in listAddC)
                    {
                        string[] values = item.Replace("{", "").Replace("}", "").Replace("(","").Replace(")", "").Replace("（", "").Replace("）", "").Split('|');
                        double vk = double.Parse(values[1]);
                        string[] vs = values[0].Split(',');
                        List<int> v = new List<int>();
                        foreach (var vl in vs)
                        {
                            v.Add(int.Parse(vl));
                        }
                        if (!adddicV.Keys.Contains(vk))
                            adddicV.Add(vk, v);
                        addDic = adddicV.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                        //addDic = adddicV.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                    }
                }
            }
        }

        public override void SetRebarDistribute()
        {
            double d1, d2;
            double d1r, d2r;
            switch (SubType)
            {
               
                case eSubType.BM:

                    d1 = (Length - Radius * 2) / 8;
                    d2 = (Width - Radius * 2) / 7;
                    //d1 = 167;
                    //d2 = 100;
                    //A截面参数
                    MainLBarDistribute = new RebarTendonDistribute(32, d1, 8);
                    MainLBarDistribute.Location = MainLBarDistribute.Delete(new List<int>() { 1,7 });
                    AddLBarDistribute = new RebarTendonDistribute();
                    //AddLBarDistribute.Location = MainLBarDistribute.Delete(new List<int>() { 0, 2, 4, 5, 7, 8, 10,12 });
                    MainWBarDistribute = new RebarTendonDistribute(32, d2, 7);
                    MainWBarDistribute.Location= MainWBarDistribute.Delete(new List<int>() { 0, 7 });
                    AddWBarDistribute = new RebarTendonDistribute();
                    //AddWBarDistribute.Location = MainWBarDistribute.Delete(new List<int>() { 0, 2, 5, 7 });
                    //B截面参数
                    MainLBarDistributeB = new RebarTendonDistribute(32, d1, 8);
                    MainLBarDistributeB.Location = MainLBarDistribute.Delete(new List<int>() { 1,2,4,6, 7 });
                    AddLBarDistributeB = new RebarTendonDistribute();
                    MainWBarDistributeB = new RebarTendonDistribute(32, d2, 7);
                    MainWBarDistributeB.Location = MainWBarDistribute.Delete(new List<int>() { 0,2,5, 7 });
                    AddWBarDistributeB = new RebarTendonDistribute();
                    //C截面参数
                    MainLBarDistributeC = new RebarTendonDistribute(32, d1, 8);
                    MainLBarDistributeC.Location = MainLBarDistributeC.Delete(new List<int>() { 1, 2, 4, 6, 7 });
                    //MainLBarDistributeC.Location = MainLBarDistribute.Delete(new List<int>() { 0, 7 });
                    AddLBarDistributeC = new RebarTendonDistribute();
                    MainWBarDistributeC = new RebarTendonDistribute(32, d2, 7);
                    MainWBarDistributeC.Location = MainLBarDistributeC.Delete(new List<int>() { 1, 2, 4, 6, 7 }); ;
                    AddWBarDistributeC = new RebarTendonDistribute();
                    break;               
            }
        }

        public override void SetRebarList()
        {
          
            //if (SubType == eSubType.D)
            //{
            //    SetTypeDValue();
            //}
            //else if (SubType == eSubType.C)
            //{
            //    SetTypeCValue();
            //}
            //else if (SubType == eSubType.B)
            //{
            //    AddLb = 4;
            //    AddWa = 4;
            //}
            //else
            //{
            //    AddLb = 4;
            //    AddWa = 4;
            //}
            #region 0-箍筋
            //3根承台箍筋
            int npts = GetStirrupNum();
            if (SubType == eSubType.BM)
                RebarList.Add(new MORebarRecord(1, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, (int)MainWBarDistribute.Location[1] * -2 + 32));
            else
                RebarList.Add(new MORebarRecord(1, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, (int)MainWBarDistribute.Location[1] * -2 + 32));
            //RebarList.Add(new MORebarRecord(2, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, (int)MainWBarDistribute.Location[3] * -2 + 32));
            int tmpn = 3;
            int tmp2 = 3;
            switch (SubType)
            {
                case eSubType.A:
                case eSubType.B:
                    tmpn = 3;
                    tmp2 = 3;
                    break;
                case eSubType.C:
                    tmpn = 3;
                    tmp2 = 3;
                    break;
                case eSubType.D:                
                    tmpn = 3;
                    tmp2 = 3;
                    break;
            }
            if (SubType == eSubType.BM)
                RebarList.Add(new MORebarRecord(2, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, (int)MainWBarDistribute.Location[tmp2] * -2 + 32));
            else
                RebarList.Add(new MORebarRecord(2, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, (int)MainWBarDistribute.Location[tmp2] * -2 + 32));

            if (SubType == eSubType.BM)
                RebarList.Add(new MORebarRecord(3, 0, "", eDB.DB12, npts * 2, eShapeType.MO, Width - 100 - 24, 264+32));
            else
                RebarList.Add(new MORebarRecord(3, 0, "", eDB.DB12, npts * 2, eShapeType.MO, Width - 100 - 24, MainLBarDistribute.Distance(0, tmpn) + 32));

            RebarList.Add(new MORebarRecord(4, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, Width - 100 - 24, 138));
            #endregion

            #region 1-获取承台尺寸
            FootingBase theFooting = MemberFactory.CreatMember(MemberFactory.FromColumnToFooting(ColumnType), SubType);
            int FTHeight = theFooting.Height;
            int FTWidth = theFooting.Width;
            int FTLen = theFooting.Length;
            int db = (int)MainRebarDB;
            #endregion

            #region 2-起步筋

            int NumOFout = 0;// 外向起步筋
            int NumOFAddout = 0;// 外向起步筋
            int NumMainAll = MainWBarDistributeC.Location.Count * 2 + MainLBarDistributeC.Location.Count * 2;
          
            if ((FTWidth - Width) * 0.5 >= 600)
            {
                NumOFout += MainLBarDistributeC.Location.Count * 2;
                NumOFAddout+= MainLBarDistribute.Location.Count * 2- MainLBarDistributeC.Location.Count * 2; 
            }
            if ((FTLen - Length) * 0.5 >= 600)
            {
                NumOFout += MainWBarDistributeC.Location.Count * 2;
                NumOFAddout += MainWBarDistribute.Location.Count * 2 - MainWBarDistributeC.Location.Count * 2;
            }
            
            string no5 = "a";
            if (NumMainAll - NumOFout > 0)
            {
                RebarList.Add(new MHXRebarRecord(5, 1, no5, MainRebarDB, true, NumMainAll - NumOFout, eShapeType.MHX));
                no5 = "b";
            }
            //5.1b为外侧起步筋；
            if (NumOFout != 0)
            {
                if (no5 == "a")
                    no5 = "";
                RebarList.Add(new MHXRebarRecord(5, 1, no5, MainRebarDB, false, NumOFout, eShapeType.MHXe));
            }
            #endregion


            #region 3-锚固筋
             if (SubType == eSubType.BM)
            {
                SetTypeBMValue();
                //int AA = ((int)(((PierHeight - 160 - FTHeight) / 3+FTHeight-160)*0.01))*100;
                //no5 = "a";
                ////if (NumMainAddAll - NumOFAddout > 0)
                ////{
                ////    RebarList.Add(new MHXRebarRecord(6, 1, no5, MainRebarDB, true, NumMainAddAll - NumOFAddout, eShapeType.MHX,H));
                ////    no5 = "b";
                ////}
                //////5.1b为外侧起步筋；
                ////if (NumOFAddout != 0)
                ////{
                ////    if (no5 == "a")
                ////        no5 = "";
                ////    RebarList.Add(new MHXRebarRecord(6, 1, no5, MainRebarDB, false, NumOFAddout, eShapeType.MHXe,H));
                ////}
                ////int AA = FTHeight - 160 + 5000;
                //int BB = 600;
                //int rr = RebarKnowledge.GerRadius(eDB.DB32);
                //RebarList.Add(new MLADDRebarRecord(6, 1, no5, MainRebarDB, NumMainAddAll, eShapeType.MLADD, AA, BB, rr));

            }
            #endregion

        }

   
        private void SetTypeBMValue()
        {
          
            FootingBase theFooting = MemberFactory.CreatMember(MemberFactory.FromColumnToFooting(ColumnType), SubType);
            int FTHeight = theFooting.Height;
            int FTWidth = theFooting.Width;
            int FTLen = theFooting.Length;
            int db = (int)MainRebarDB;
            int NumMainAll = MainWBarDistributeC.Location.Count * 2 + MainLBarDistributeC.Location.Count * 2;
            int NumMainAllA = MainWBarDistribute.Location.Count * 2 + MainLBarDistribute.Location.Count * 2;
            int NumMainAllB = MainWBarDistributeB.Location.Count * 2 + MainLBarDistributeB.Location.Count * 2;
            #region 3-锚固筋

            int Z1 = 8494 - (FTHeight - 160) - 1200; // 搭接节点 1//Z1=8483-(H-160)-1200
            int L2 = 0;// 盖梁锚固筋长度；
            int L12Num = 0;
            bool isDiv = false;

            int aa = (int)(PierHeight / 3);
            if (PierHeight - Z1 + 1200 + 1500 > 12000)
            {
                // 分3节
                L12Num = NumMainAll;
                isDiv = true;
                //L2 = (PierHeight - Z1 + 1200 + 1500 - aa);
                L2 = (PierHeight - Z1 + 1200 - aa);
                if (L2 + 1200 * 2 > 12000 - 45 + 32)
                {
                    aa += L2 + 1200 * 2 - (12000 - 45 + 32);
                    L2 = 12000 - 45 + 32 - 2400;
                }
            }
            else
            {
                int numA = MainWBarDistribute.Location.Count;
                // 分2节
                L2 = 1500 + (PierHeight - Z1) + 1200; // 深入盖梁钢筋总长度    
                aa = L2 - 1500;
            }

            CrossbeamBase theCB = MemberFactory.CreatMember(MemberFactory.ColumnToCrossBeam(ColumnType), 0);
            NumMainAll = MainLBarDistributeC.Location.Count * 2 + MainWBarDistributeC.Location.Count * 2;
            int NumL = 0;//外侧L数量

            int NumS = 0;//直线
            int bb = 0;
            int cc = 0;//1500-(B-r/2-d);
            int dd = 0;
            
            int rr = RebarKnowledge.GerRadius(eDB.DB32);
            string no = "a";
            // 5.2a为L锚固
            // 是否与预应力冲突 ？Y:B=100
            if (Math.Abs(Length - 50 - 12 - 16 - theCB.TendonDistribute.Location[0] * -1) <= theCB.TendonDistribute.Diameter * 0.5)
            {
                // 外侧主筋L型避让
                bb = 100;
                cc = (int)(1500 - bb - rr * 0.5 - 32);
                NumL += MainWBarDistributeC.Location.Count * 2;
            
            }
            else
            {
               
                bool isEixt = false;//是否在安全距离
                foreach (double item in MainLBarDistribute.Location.Values)
                {
                    //第一个对比
                    if (Math.Abs(item) > theCB.L2 * 0.5 - 82)
                    {
                        isEixt = true;
                    }
                    break;

                }
              
                NumL += MainWBarDistributeC.Location.Count * 2;
            }
            int NumLSZ = 0;
           
            int numL = 0, numS = 0; //避开预应力的主筋直线和L数量
            int numLC = 0, numSC = 0;
            int NumLC = 0;//不冲突的
            int i = 0;
           



            NumS = 2 * 2;

            numL = 2;//写死
            NumL += numL * 2 ;
     
            //其他
            int NumLS = NumMainAll - NumS - NumL;//闪电型         
            if (NumS > 0)
            {
                //直线锚固
                RebarList.Add(new MLSRebarRecord(5, 2, no, MainRebarDB, NumS, eShapeType.MLS, aa, 1500));
                if (no == "a")
                    no = "b";
                else if (no == "b")
                    no = "c";
                else if (no == "c")
                    no = "d";
                else if (no == "d")
                    no = "e";
                else if (no == "e")
                    no = "f";
                else if (no == "f")
                    no = "g";
                else
                    no = "h";
            }
            if (NumLS > 0)
            {
                bb = 100;
                cc = 180;
                dd = 1500 - 100 - 180;
                int numAll = 0;
                foreach (var key in dic.Keys)
                {
                    numAll += dic[key].Count * 2;
                }
                if (numAll > 0)
                {
                    foreach (var key in dic.Keys)
                    {
                        cc = (int)key;
                        dd = 1500 - 100 - (int)key;
                        int count = dic[key].Count * 2;
                        //闪电锚固
                        RebarList.Add(new MLZRebarRecord(5, 2, no, MainRebarDB, count, eShapeType.MLZ, aa, bb, cc, dd));
                        if (no == "a")
                            no = "b";
                        else if (no == "b")
                            no = "c";
                        else if (no == "c")
                            no = "d";
                        else if (no == "d")
                            no = "e";
                        else if (no == "e")
                            no = "f";
                        else if (no == "f")
                            no = "g";
                        else
                            no = "h";
                    }
                }
                else
                {
                    RebarList.Add(new MLZRebarRecord(5, 2, no, MainRebarDB, NumLS, eShapeType.MLZ, aa, bb, cc, dd));
                    if (no == "a")
                        no = "b";
                    else if (no == "b")
                        no = "c";
                    else if (no == "c")
                        no = "d";
                    else if (no == "d")
                        no = "e";
                    else if (no == "e")
                        no = "f";
                    else if (no == "f")
                        no = "g";
                    else
                        no = "h";
                }

            }
            if (NumL > 0)
            {
                //L锚固

                if (NumL - NumLC > 0)
                {
                    bb = theCB.CalculatedHeight - 100;
                    cc = (int)(1500 - bb + rr * 0.5 + 32);
                    RebarList.Add(new MLRebarRecord(5, 2, no, MainRebarDB, NumL - NumLC, eShapeType.ML, aa, bb, cc, rr));
                    if (no == "a")
                        no = "b";
                    else if (no == "b")
                        no = "c";
                    else if (no == "c")
                        no = "d";
                    else if (no == "d")
                        no = "e";
                    else if (no == "e")
                        no = "f";
                    else if (no == "f")
                        no = "g";
                    else
                        no = "h";
                }
                if (NumLC > 0)
                {
                    bb = 100;
                    cc = (int)(1500 - bb + rr * 0.5 + 32);
                    RebarList.Add(new MLRebarRecord(5, 2, no, MainRebarDB, NumLC, eShapeType.ML, aa, bb, cc, rr));
                    if (no == "a")
                        no = "b";
                    else if (no == "b")
                        no = "c";
                    else if (no == "c")
                        no = "d";
                    else if (no == "d")
                        no = "e";
                    else if (no == "e")
                        no = "f";
                    else if (no == "f")
                        no = "g";
                    else
                        no = "h";
                }
            }
            //if (NumL > 0)
            //{
            //    //L锚固


            //        bb = theCB.CalculatedHeight - 100;
            //        cc = (int)(1500 - bb + rr * 0.5 + 32);
            //        RebarList.Add(new MLRebarRecord(5, 2, no, MainRebarDB, NumL - NumLC, eShapeType.ML, aa, bb, cc, rr));
            //        if (no == "a")
            //            no = "b";
            //        else if (no == "b")
            //            no = "c";
            //        else if (no == "c")
            //            no = "d";
            //        else if (no == "d")
            //            no = "e";
            //        else if (no == "e")
            //            no = "f";
            //        else if (no == "f")
            //            no = "g";
            //        else
            //            no = "h";

            //}
            if (isDiv)
            {
                RebarList.Add(new MSZRebarRecord(5, 3, "", MainRebarDB, NumMainAll, eShapeType.MSZ, L2 + 1200 * 2));
            }
            #endregion
            #region 加强筋
            aa = (int)(PierHeight/3);
            int NumMainAddAll = MainWBarDistribute.Location.Count * 2 + MainLBarDistribute.Location.Count * 2 - (MainWBarDistributeC.Location.Count * 2 + MainLBarDistributeC.Location.Count * 2);
            int NumAddS = 3 * 2;
            RebarList.Add(new MLSRebarRecord(6, 1, "a", MainRebarDB, NumAddS, eShapeType.MLS, aa, 1500));
            int NumAddL = NumMainAddAll - NumAddS;
            if (NumAddL > 0)
            {
                bb = theCB.CalculatedHeight - 100;
                cc = (int)(1500 - bb + rr * 0.5 + 32);
                RebarList.Add(new MLRebarRecord(6, 1, "b", MainRebarDB, NumAddL, eShapeType.ML, aa, bb, cc, rr));
            }
            #endregion
            #region UBlot数量计算
            if (isDiv)
                UAN1N2 = NumMainAll * 8;
            else
                UAN1N2 = NumMainAll * 4;
            if (isDiv)
            {
                UboltList.UBoltNum[MainRebarDB] += NumMainAll * 8;
            }
            else
            {
                UboltList.UBoltNum[MainRebarDB] += NumMainAll * 4;
            }
            #endregion
        }
    }

}
